function writecell(celmat,into); 
% function writecell(celmat,into);
%
% Writes the entries of a cell to an ascii file
% Inputs
% ------
% celmat matrix of cells ( use num2cell.m ) 
% into.fid  fid for file to write 
% into.fmat format for the column of celmat 
% If into.fmat = one entry, use the same for each column
%              = row vector, use specified format 
%                for each column 
%              = [] automatically determine 
%                minimum optimal format for each 
%                column depending on whether string 
%                or cell content 
%
% Note: Need to call fclose to close the file 
%       Use %- for left justified
% Alejandro Justiniano  6/9/03
% *******************************************************

% Define format string for columns of the cell  
fmat=into.fmat;
fmind=sum( size(fmat) ); 
if fmind == 2
    rfm1=['%',num2str(fmat),'s','\t'];
    rfm2=[rfm1,' '];
    rfm=repmat(rfm2,1, size(celmat,2)-1);
    rfm=[rfm,rfm1,'\n']; 
else;
    numc=size(celmat,2); 
    % Create format vector if not provided 
    if fmind == 0 
        fmat=zeros(1,numc);
        kk=1; 
        for kk=1:numc; 
            cols=celmat(:,kk);
            dtype=str2num( cols{1,:} ) ;
            if isempty(dtype) == 1; %Entry is a String
                fmat(kk)=size(char(cols),2); 
            else % Entry is a number 
                fmat(kk)=size(char(cols),2) + 0.5; 
            end
        end
    end
    % Write format Strings     
    jj=1; 
    rfm=[]; 
    while jj<=(numc-1);           
        rfm=[rfm,'%',num2str(fmat(jj)),'s','\t',' ']; 
        jj=jj+1; 
    end
    rfm=[rfm,'%',num2str(fmat(jj)),'s','\t\n']; 
end
% Write the cell 
ii=1;  
while ii<=size(celmat,1); 
    fprintf(into.fid,rfm,celmat{ii,:});
    ii=ii+1; 
end; 